package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.CategoryChannelBo;
import com.ruoyi.supplier.domain.vo.CategoryChannelVo;
import com.ruoyi.supplier.domain.CategoryChannel;
import com.ruoyi.supplier.mapper.CategoryChannelMapper;
import com.ruoyi.supplier.service.ICategoryChannelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频号商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class CategoryChannelServiceImpl implements ICategoryChannelService {

    private final CategoryChannelMapper baseMapper;

    /**
     * 查询视频号商品分类
     */
    @Override
    public CategoryChannelVo queryById(Long categoryChannelId){
        return baseMapper.selectVoById(categoryChannelId);
    }

    /**
     * 查询视频号商品分类列表
     */
    @Override
    public TableDataInfo<CategoryChannelVo> queryPageList(CategoryChannelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CategoryChannel> lqw = buildQueryWrapper(bo);
        Page<CategoryChannelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询视频号商品分类列表
     */
    @Override
    public List<CategoryChannelVo> queryList(CategoryChannelBo bo) {
        LambdaQueryWrapper<CategoryChannel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CategoryChannel> buildQueryWrapper(CategoryChannelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CategoryChannel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, CategoryChannel::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getCatId() != null, CategoryChannel::getCatId, bo.getCatId());
        lqw.eq(bo.getFCatId() != null, CategoryChannel::getFCatId, bo.getFCatId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), CategoryChannel::getName, bo.getName());
        lqw.eq(bo.getLevel() != null, CategoryChannel::getLevel, bo.getLevel());
        lqw.eq(bo.getCreatedAt() != null, CategoryChannel::getCreatedAt, bo.getCreatedAt());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), CategoryChannel::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), CategoryChannel::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getAttr()), CategoryChannel::getAttr, bo.getAttr());
        lqw.eq(bo.getSort() != null, CategoryChannel::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, CategoryChannel::getStatus, bo.getStatus());
        lqw.eq(bo.getForAudit() != null, CategoryChannel::getForAudit, bo.getForAudit());
        return lqw;
    }

    /**
     * 新增视频号商品分类
     */
    @Override
    public Boolean insertByBo(CategoryChannelBo bo) {
        CategoryChannel add = BeanUtil.toBean(bo, CategoryChannel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryChannelId(add.getCategoryChannelId());
        }
        return flag;
    }

    /**
     * 修改视频号商品分类
     */
    @Override
    public Boolean updateByBo(CategoryChannelBo bo) {
        CategoryChannel update = BeanUtil.toBean(bo, CategoryChannel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CategoryChannel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除视频号商品分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
